﻿#region File and License Information
/*
<File>
	<License>
		Copyright © 2009 - 2017, Daniel Vaughan. All rights reserved.
		This file is part of Calcium (http://calciumsdk.net), 
		which is released under the MIT License.
		See file /Documentation/License.txt for details.
	</License>
	<CreationDate>2010-11-20 13:28:27Z</CreationDate>
</File>
*/
#endregion

using System;
using System.Windows;

using Microsoft.Phone.Controls;
using Microsoft.Phone.Tasks;

namespace Outcoder.UI.Xaml.Controls
{
    public class AppBarHyperlinkButton : AppBarIconButtonBase
    {
        public static readonly DependencyProperty NavigateUriProperty
            = DependencyProperty.RegisterAttached(
                "NavigateUri",
                typeof(Uri),
                typeof(AppBarHyperlinkButton),
                new PropertyMetadata(HandleNavigateUriChanged));

        public Uri NavigateUri
        {
            get
            {
                return (Uri)GetValue(NavigateUriProperty);
            }
            set
            {
                SetValue(NavigateUriProperty, value);
            }
        }

        static void HandleNavigateUriChanged(DependencyObject d,
            DependencyPropertyChangedEventArgs e)
        {
        }

        protected override void OnClick(EventArgs e)
        {
            base.OnClick(e);
            Navigate();
        }

        void Navigate()
        {
            var uri = NavigateUri;
            if (uri == null)
            {
                return;
            }

            var frame = Application.Current.RootVisual as PhoneApplicationFrame;
            if (frame == null)
            {
                return;
            }

            if (uri.IsAbsoluteUri)
            {
                WebBrowserTask task = new WebBrowserTask { Uri = uri };
                task.Show();
                return;
            }

            frame.Navigate(uri);
        }
    }
}